﻿using System.Collections;
using System.Windows;
using PeoplesJukebox.Web.Entities;
using PeoplesJukebox.Web.Enums;
using PeoplesJukebox.Web.ServiceClients;
using PeoplesJukebox.Web.UserControls;

namespace PeoplesJukebox.Web.Pages
{
    /// <summary>
    /// Page showing a random selection of track suggestions
    /// </summary>
    public partial class SuggestPage : PageBase
    {
        /// <summary>
        /// Returns the page type
        /// </summary>
        public override PageType PageType
        {
            get { return PageType.Suggest; }
        }

        /// <summary>
        /// Default constructor
        /// </summary>
        public SuggestPage()
        {
            InitializeComponent();
            this.CatalogueServiceClient.SearchComplete += new CatalogueServiceClient.SearchCompleteEventHandler(CatalogueServiceClient_SearchComplete);
            this.resultsList.TrackSelected += new ResultsList.OnTrackSelectedEventHandler(resultsList_TrackSelected);
        }

        /// <summary>
        /// Resets the page state
        /// </summary>
        public override void Reset()
        {
            base.Reset();
            this.resultsList.Clear();
            //fill the page on startup:
            this.CatalogueServiceClient.GetTrackSuggestions();
        }

        private void resultsList_TrackSelected(MediaItem track)
        {
            this.DataContext = track;
            this.Completed(PageType.ConfirmTrackSelection);
        }

        private void CatalogueServiceClient_SearchComplete(IEnumerable results)
        {
            this.resultsList.Results = results;
        }

        private void btnSuggest_Click(object sender, RoutedEventArgs e)
        {
            this.CatalogueServiceClient.GetTrackSuggestions();
        }
    }
}